package com.uppfind.framework.core.service.impl;

import com.uppfind.framework.core.bean.GenericPo;
import com.uppfind.framework.core.dao.api.InsertMapper;
import com.uppfind.framework.core.dao.api.common.params.InsertParam;
import com.uppfind.framework.core.service.InsertService;
import com.uppfind.framework.utils.ValidationUtil;

/**
 * Created by xuliugen on 2017/6/29.
 */
public interface SimpleInsertService<PO extends GenericPo<PK>, PK> extends InsertService<PO, PK> {

    InsertMapper getInsertMapper();

    Class<PK> getPKType();

    @Override
    default PK insert(PO po) {
        if (getPKType() == String.class && po.getId() == null) {
            ((GenericPo<String>) po).setId(GenericPo.createUID());
        }
        //JSR303 校验
        ValidationUtil.getInstance().validateParams(po);
        getInsertMapper().insert(InsertParam.build(po));
        return po.getId();
    }
}
